This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
library(OpasnetUtils)
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
#Paketti datan editoimiseen ja visualisointiin
library(tidyverse)
## ── Attaching packages ────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ✓ purrr 0.3.4
## ── Conflicts ───────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::combine() masks OpasnetUtils::combine()
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag() masks stats::lag()
if(FALSE){
#Paketti datan lukemiseen
library(jsonlite)
library(rjson)
#Luetaan aineisto sisään
aineisto <- fromJSON("https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.json?row=dateweek2020010120201231-443686&column=hcdmunicipality2020-445222")
dat <- read.csv("https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.csv?row=dateweek2020010120201231-443686&row=hcdmunicipality2020-445222&column=measure-141082", sep=";")
dat <- read.csv("https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.csv?row=dateweek2020010120201231-443686&row=hcdmunicipality2020-445131&column=measure-141082", sep=";")
meta <- readLines("https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.dimensions.json")
} # ENDIF
#Paketti datan lukemiseen
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
##
## flatten
library(rjson)
##
## Attaching package: 'rjson'
## The following objects are masked from 'package:jsonlite':
##
## fromJSON, toJSON
dat <- jsonlite::fromJSON("https://plus.yle.fi/lambda_sheets/korona/2020-04-municipalities-infections-cumulative/data.json")
dat <- as.data.frame(dat$data)
dat$date <- as.Date(dat$date, format="%d.%m.%Y")
dat$cumulative[dat$cumulative==".."] <- 0
dat$cumulative <- as.numeric(as.character(dat$cumulative))
dat$new <- as.numeric(as.character(dat$new))
dat$perhundredthousand <- as.numeric(as.character(dat$perhundredthousand))
geojson <- rjson::fromJSON(file="https://github.com/teelmo/geodata/raw/master/geojson/Kuntarajat%202017.geojson")
g <- list(
fitbounds = "geojson",
projection="albers",
visible = FALSE
)
### Kartta tartuttavista (eli viimeisten X päivän tapauksista)
observation_window <- 14
tmp <- dat[dat$date>=max(dat$date)-observation_window,]
tmp <- aggregate(tmp$new, by=tmp[c("area")], FUN=function(x) sum(x, na.rm=TRUE))
### Kartta kaikista raportoiduista tapauksista
fig <- plot_ly() %>%
add_trace(
type="choropleth",
geojson=geojson,
locations=tmp$area,
z=tmp$cumulative,
zmin=0,
zmax=50,
colorscale="Viridis",
featureidkey="properties.Name"
) %>%
layout(
geo = g
)
fig
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
tmp <- tmp[order(-tmp$x),]
cat("There are", sum(tmp$x), "infective corona cases in Finland right now.\n")
## There are 135 infective corona cases in Finland right now.
tmp
############################################
if(FALSE){
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- read.csv(url2, colClasses=c(fips="character"))
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
fig <- plot_ly()
fig <- fig %>% add_trace(
type="choropleth",
geojson=counties,
locations=df$fips,
z=df$unemp,
colorscale="Viridis",
zmin=0,
zmax=12,
marker=list(line=list(
width=0)
)
)
fig <- fig %>% colorbar(title = "Unemployment Rate (%)")
fig <- fig %>% layout(
title = "2016 US Unemployment by County"
)
fig <- fig %>% layout(
geo = g
)
fig
#################################################
shp <- substr(meta[grep("SHP",meta)-1],8,13)
day <- substr(meta[grep(':"week"',meta)-2],8,13)
l1 <- "https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.csv?row=dateweek2020010120201231-"
l1j <- "https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.json?row=dateweek2020010120201231-"
l2 <- "&row=hcdmunicipality2020-"
l3 <- "&column=measure-141082"
out <- data.frame()
for(i in shp) {
for(j in day) {
out <- rbind(out, read.csv(paste0(l1, j, l2, i, l3),sep=";"))
}
}
out <- data.frame()
for(i in shp) {
for(j in "443686") {
out <- rbind(out, read.csv(paste0(l1, j, l2, i, l3),sep=";"))
}
}
dat <- out
dat$val <- as.numeric(as.character(dat$val))
dat <- na.omit(dat)
dat$Time <- as.numeric(substr(dat$Aika,19,20))
dat$Day <- as.Date(dat$Aika)
library(ggplot2)
library(plotly)
plot_ly(dat[dat$Mittari=="Tapausten lukumäärä",], x=~Time, y=~val, color=~Alue, type="scatter", mode="markers&lines") %>%
layout(title="Tapausten lukumäärä")
ggplot(dat[dat$Mittari=="Tapausten lukumäärä" & !grepl("SHP",dat$Alue),], aes(x=Time, y=val,colour=Alue))+geom_line()+facet_wrap(~Mittari)
ggplot(dat[dat$Mittari=="Tapausten lukumäärä",], aes(x=Day, y=val,colour=Alue))+geom_line()+facet_wrap(~Mittari)
tmp <- data.frame(value=unlist(aineisto$dataset$value))
tmp$value <- as.numeric(tmp$value)
tmp <- rownames_to_column(tmp)
tmp <- merge(data.frame(rowname=1:prod(aineisto$dataset$dimension$size)),tmp,all=TRUE)
tmp <- matrix(tmp$value,nrow=aineisto$dataset$dimension$size[2], dimnames=list(
unlist(aineisto$dataset$dimension$hcdmunicipality2020$category$label),
unlist(aineisto$dataset$dimension$dateweek2020010120201231$category$label)
))
#puretaan kategoriat paloiksi
tmp <- aineisto$dataset$dimension$dateweek2020010120201231$category
label <- data.frame(
label=unlist(tmp$label),
index=unlist(tmp$index),
labelname=unlist(names(tmp$label)),
indexname=unlist(names(tmp$label))
)
#Nimetään palaset
names(label)<-"label"
names(index)<-"index"
#Laitetana vielä rivinumerot sarakkeiksi, jotta nämä saadaan yhteen.
label<-rownames_to_column(label)
index<-rownames_to_column(index)
#Yhdistetään rivinimeä käyttäen
kategoriat <- index %>% left_join(label,by="rowname")
#otetaan data
data <- as.data.frame(unlist(aineisto$dataset$value))
#Nimetään
names(data)<-"Tapauksien lkm"
data<-rownames_to_column(data)
data$rowname<-as.numeric(data$rowname)
#Yhdistetään muuhun aineistoon
dataset <- kategoriat %>% left_join(data,by=c("index"="rowname"))
} # ENDIF
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.